//
//  PicViewController.m
//  YouGoWeGo
//
//  Created by LiuFei on 14/11/19.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "PicViewController.h"

#import "CommViewController.h"
#import "JourneyViewController.h"
#import "CustomPicView.h"
#import "AreaModel.h"
#import "UIImageView+WebCache.h"
#import "Database.h"


@interface PicViewController ()<UIScrollViewDelegate>

@property (retain, nonatomic) UIActivityIndicatorView *ac;

@end

@implementation PicViewController

- (void)dealloc
{
    [_albumArray release];
    [_pInfoDic release];
    [_picData release];
    [_text release];
    [_trip_id release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        self.pInfoDic = [NSMutableDictionary dictionary];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationItem setTitle:@"图片浏览"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
//    [self createView];
    [self createRightButton];
    [self createScroll];
    [self createSubViews];
}

#pragma mark - 创建右按钮
- (void)createRightButton
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    [button setTitle:@"查看旅程" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickRightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    [button release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)createScroll
{
    self.bigScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 100)];
    [_bigScroll setContentSize:CGSizeMake(self.view.frame.size.width * [_albumArray count], 0)];
    [_bigScroll setDelegate:self];
    [_bigScroll setScrollEnabled:YES];
    [_bigScroll setPagingEnabled:YES];
    [_bigScroll setDecelerationRate:UIScrollViewDecelerationRateNormal];
    [_bigScroll setShowsHorizontalScrollIndicator:YES];
    [self.view addSubview:_bigScroll];
    
    
    for (int i = 0; i < [_albumArray count]; i ++) {
        
        UIScrollView *smallScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.bigScroll.frame.size.height - 100)];
        [smallScroll setContentSize:CGSizeMake(_bigScroll.frame.size.width, self.view.frame.size.height / 2)];
        
        [smallScroll setScrollEnabled:YES];
        [smallScroll setDelegate:self];
        [smallScroll setMaximumZoomScale:3];
        [smallScroll setMinimumZoomScale:0.5];
        [smallScroll setZoomScale:1];
        [smallScroll setShowsHorizontalScrollIndicator:YES];
        
        NSMutableDictionary *pinfoDic = [self.pInfoArray objectAtIndex:i];
        NSString *str = [pinfoDic objectForKey:@"photo_info"];
        CGFloat image_h = 0;
        CGFloat image_w = 0;
        if ([str length] == 6) {
            image_h = [[str substringWithRange:NSMakeRange(0, 3)] floatValue];
            image_w = [[str substringWithRange:NSMakeRange(3, 3)] floatValue];
        } else {
            image_h = [[str substringWithRange:NSMakeRange(0, 4)] floatValue];
            image_w = [[str substringWithRange:NSMakeRange(4, 4)] floatValue];
        }
        
//        AreaModel *model = [self.albumArray objectAtIndex:i];
        NSMutableDictionary *dic = [_albumArray objectAtIndex:i];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, image_h * self.view.frame.size.width / image_w)];
        image.center = _bigScroll.center;
        [image setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"photo_s"]]];
        [smallScroll addSubview:image];
        [_bigScroll addSubview:smallScroll];
        [image release];
        [smallScroll release];
    }
    
    [_bigScroll release];
}


- (void)createSubViews
{
    UIButton *commButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 3, self.bigScroll.frame.size.height, 60, 30)];
    [commButton setTitle:@"评论" forState:UIControlStateNormal];
    [commButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [commButton setTag:1004];
    [commButton addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commButton];
    [commButton release];
    
    UIButton *colButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 3 * 2, self.bigScroll.frame.size.height, 60, 30)];
    [colButton setTitle:@"喜欢" forState:UIControlStateNormal];
    [colButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [colButton setTag:1005];
    [colButton addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:colButton];
    [colButton release];
    
//    CGPoint point = _bigScroll.contentOffset;
//    NSInteger n = point.x / self.view.frame.size.width;
    
    for (int i = 0; i < [_albumArray count]; i ++) {
        NSMutableDictionary *dic = [self.albumArray objectAtIndex:i];
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 + self.view.frame.size.width * i, colButton.frame.size.height + 370, self.view.frame.size.width - 20, 50)];
        
        
        [contentLabel setText:[dic objectForKey:@"text"]];
        [contentLabel setTextColor:[UIColor blackColor]];
        [self.bigScroll addSubview:contentLabel];
        [contentLabel release];
    }
    
    
}

#pragma mark - 创建自定义view
//- (void)createView
//{
//    CustomPicView *cView = [[CustomPicView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
//    [cView setBackgroundColor:[UIColor orangeColor]];
//    
//    [cView.picImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", _picUrl]]];
//    
//    cView.contentLabel.text = _text;
//    
//    [cView.commButton addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    
//    [cView.collectButton addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [cView.picImage setUserInteractionEnabled:YES];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showAlbum:)];
//    [cView.picImage addGestureRecognizer:tap];
//    
//    [self.view addSubview:cView];
//    [tap release];
//    [cView release];
//}

#pragma mark - 点击事件
- (void)clickButtonAction:(id)sender
{
    UIButton *button = (UIButton*)sender;
    switch (button.tag) {
        case 1004:
        {
            CommViewController *commVC = [[CommViewController alloc] init];
            CGPoint point = _bigScroll.contentOffset;
            NSInteger n = point.x / self.view.frame.size.width;
            
            NSMutableDictionary *dic = [self.albumArray objectAtIndex:n];

            commVC.ID = [dic objectForKey:@"id"];
            commVC.trip_id = _trip_id;
            commVC.picUrl = [dic objectForKey:@"photo_s"];
            
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:commVC];
            
            [self presentViewController:nav animated:YES completion:nil];
            [commVC release];
        }
            break;
        case 1005:
        {
            NSString *pInfo = [self.pInfoDic objectForKey:@"photo_info"];
            
            CGPoint point = _bigScroll.contentOffset;
            NSInteger n = point.x / self.view.frame.size.width;
            NSMutableDictionary *dic = [self.albumArray objectAtIndex:n];
            
            Model *model = [Model modelWithId:[[dic objectForKey:@"id"] integerValue] title:pInfo Data:[dic objectForKey:@"photo_s"]];
            NSLog(@"%lf", point.x);
            NSLog(@"n = %ld", n);
            [Database insertWithModel:model table:@"Picture"];
//            [str release];
        }
        default:
            break;
    }
}

- (void)clickRightButtonAction:(id)sender
{
    JourneyViewController *jourVC = [[JourneyViewController alloc] init];
    
    jourVC.trip_id = _trip_id;
    
    [self.navigationController pushViewController:jourVC animated:YES];
    [jourVC release];
}

//- (void)showAlbum:(UITapGestureRecognizer *)tap
//{
//    PicAlbumViewController *pAlbum = [[PicAlbumViewController alloc] init];
//    pAlbum.albumArray = self.albumArray;
//    pAlbum.photo_info = self.pInfoDic;
//    pAlbum.pInfoArray = self.pInfoArray;
//    [pAlbum view];
//    [self.navigationController pushViewController:pAlbum animated:YES];
//    [pAlbum.bigScroll setContentOffset:CGPointMake(_index * self.view.frame.size.width, 0)];
//
//    [pAlbum release];
//}


@end
